package cosc603.server;

import cosc603.client.GreetingService;
import cosc603.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"You must input the Text!");
		}

		return input;
	}


	public String stutter(String messageInput) {
		
		int lineNum = 1;
		int count = 0;
		boolean chkStutter = false;
		String[] byLine;
		String temp = "";
		String st = "";
		String inputString = messageInput;
		byLine = inputString.split("\n");
		while (count < byLine.length ) {
			String line = byLine[count].replaceAll("[^a-zA-Z0-9 ]", "");

			String[] splitLine = line.split(" ");
			
			if (splitLine[0].equals(temp)){
				st = st + "Repeated word on line " + lineNum + " : " + temp + "<br>";
				chkStutter = true;

			}
			for (int i = 0; i < splitLine.length - 1; i++) {
				if (splitLine[i].equals(splitLine[i + 1])){
					st = st +  "Repeated word on line " + lineNum + " : " + splitLine[i] + "<br>";
					chkStutter = true;
					
				}
				
				
			}
			temp = splitLine[splitLine.length-1];
			count++;
			lineNum++;
		}
		if(!chkStutter){
			st = "No repeated words found";
		}
		return st;
	}
}
